package com.roy.sms.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roy.sms.service.UserService;
import com.roy.sms.util.PageParam;
import com.roy.sms.util.Pager;
import com.roy.sms.vo.UserVO;

@Controller
public class UserController extends BaseController{

	@Resource
	private UserService userService;
	
	@RequestMapping("/user/manager.do")
	public String manager(){
		return "user/userManager";
	}
	
	@RequestMapping("/user/list.do")
	@ResponseBody
	public Pager<UserVO> list(PageParam pageParam){
		return userService.findPage(pageParam.getRowBeginIndex(), pageParam.getRowCount());
	}
	
	@RequestMapping("/user/initEdit.do")
	public String initEdit(Integer id, ModelMap modelMap){
		UserVO userVO = null;
		if(id != null){
			userVO = userService.loadUser(id);
		} else {
			userVO = new UserVO();
		}
		modelMap.put("userVO", userVO);
		return "user/editUser";
	}
	
	@RequestMapping("/user/saveUser.do")
	@ResponseBody
	public String saveUser(UserVO userVO){
		if(userVO.getId() == 0){
			userService.add(userVO);
		} else {
			userService.update(userVO);
		}
		return SUCCESS;
	}
	
	@RequestMapping("/user/deleteUsers.do")
	@ResponseBody
	public String deleteUsers(String ids){
		userService.deleteUsers(ids);
		return SUCCESS;
	}
	
	@RequestMapping("/user/selectUser.do")
	public String selectUser(){
		return "user/selectUser";
	}
}
